local skel = fk.CreateSkill {
  name = "emo__xueyan",
  tags = {Skill.Compulsory},
}

local EMO = require "packages.moepack.utility.emo_util"

skel:addEffect(fk.Damaged, {
  priority = 1.001,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:addPlayerMark(player, "@emo__xueyan", data.damage)
  end,
})

skel:addEffect(fk.TurnStart, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and player:getMark("@emo__xueyan") > 0
  end,
  on_cost = function (self, event, target, player, data)
    local x = math.min(3, player:getMark("@emo__xueyan"))
    local num = EMO.chooseNum(player, 1, x, "#emo__xueyan-cost")
    if num then
      event:setCostData(self, {num = num})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local num = event:getCostData(self).num
    room:removePlayerMark(player, "@emo__xueyan", num)
    room:addPlayerMark(player, "emo__yangsan_used-turn", num)
    local chosens = {}
    local all_choices = {"emo__xueyan_draw", "emo__xueyan_recover", "emo__xueyan_recast"}
    for i = 1, num do
      if player.dead then return end
      local choices = table.filter(all_choices, function (ch) return not table.contains(chosens, ch) end)
      local wound = #table.filter(room.alive_players, function (p) return p:isWounded() end)
      if wound == 0 then table.removeOne(choices, "emo__xueyan_draw") end
      if not player:isWounded() then table.removeOne(choices, "emo__xueyan_recover") end
      if #choices == 0 then break end
      local choice = room:askToChoice(player, {
        choices = choices, all_choices = all_choices, skill_name = skel.name, prompt = "#emo__xueyan-choice",
      })
      table.insert(chosens, choice)
      if choice == "emo__xueyan_draw" then
        player:drawCards(wound, skel.name)
        room:addPlayerMark(player, MarkEnum.AddMaxCardsInTurn, wound)
      elseif choice == "emo__xueyan_recover" then
        room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
      elseif choice == "emo__xueyan_recast" then
        room:damage { to = player, damage = 1, skillName = skel.name }
        if not player.dead and not player:isAllNude() then
          local cards = {}
          if #player:getCardIds("j") == 0 then
            cards = room:askToCards(player, {
              min_num = 1, max_num = 9999, include_equip = true, cancelable = true, skill_name = skel.name,
              prompt = "#emo__xueyan-recast",
            })
          else
            cards = room:askToChooseCards(player, {
              target = player, flag = "hej", min = 0, max = 9999, skill_name = skel.name,
            })
          end
          if #cards > 0 then
            room:recastCard(cards, player, skel.name)
          end
        end
      end
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  if not (player:hasSkill("emo__xueyan", true) or player:hasSkill("emo__yangsan", true) or player:hasSkill("emo__luomu", true)) then
    player.room:setPlayerMark(player, "@emo__xueyan", 0)
  end
end)

Fk:loadTranslationTable{
  ["emo__xueyan"] = "血宴",
  [":emo__xueyan"] = "锁定技，有角色受到伤害后，你获得等量层血宴；你回合开始时，你可以消耗任意层血宴执行等量项：1.摸X张牌且本回合手牌上限+X（X为场上已受伤角色数）；2.回复1点体力；3.受到1点伤害，重铸你区域内的任意张牌。",
  ["#emo__xueyan-cost"] = "血宴：你可以消耗数层“血宴”执行等量项效果",
  ["#emo__xueyan-choice"] = "血宴：选择一项效果执行",
  ["@emo__xueyan"] = "血宴",
  ["emo__xueyan_draw"] = "摸牌，加手牌上限",
  ["emo__xueyan_recover"] = "回复1点体力",
  ["emo__xueyan_recast"] = "受到伤害，重铸牌",
  ["#emo__xueyan-recast"] = "重铸你任意张牌！",

  ["$emo__xueyan"] = "该吸血了。",
}

return skel
